<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>

<div th:insert="publicHeader.html"></div>
<script th:src="@{/publicHeader.js}"></script>

<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px" >

    <div class="container">

        <div class="row">
            <div class="col-md-12">
                <p style="background-color: cornflowerblue ; color: white; font-size: 30px">武器销售排行榜</p>
            </div>
        </div>

        <div class="row" style="font-size: 16px">
            <div class="col-sm-6 col-md-4" v-for="(weapon,index) in weaponList">
                <div class="thumbnail">
                    <img :src="weapon.productImgsrc" alt="..." class="col-sm-6 col-md-6" style="width: 140px;height: 130px">
                    <div class="caption text-left">
                        <p>武器排名:<span v-text="index+1"></span></p>
                        <p>武器名称:<span v-text="weapon.productName"></span></p>
                        <p>武器价格:<span v-text="weapon.productPrice"></span></p>
                        <p>
                            <a class="btn btn-primary" @click.prevent="oneWeapon(weapon.id)">详情</a> &nbsp;&nbsp;&nbsp;&nbsp;
                            <a class="btn btn-success" @click.prevent="addCart(weapon.id)">加入购物车</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>

    </div>

</div>

<script>
    console.log("---top武器---")
    let weapon = new Vue({
        el:"#app",
        data: {
            weaponList:[]
        },
        methods: {
            weaponListInfo(){
                let vueObj = this;
                $.post("/weapon/topN",{n:6},function (data) {
                    console.log(data);
                    vueObj.weaponList = data;
                })
            },
            oneWeapon(weaponId){
                console.log("详情武器id:  " + weaponId)
                sessionStorage.setItem("weaponId",weaponId)
                location.href="/singleWeapon.html";
            },
            addCart(weaponId){
                if (!weaponHeaderType.uname){
                    alert("该用户未登录,请登录后加购商品!")
                    return;
                }
                console.log("购物车时武器ID:  " + weaponId)
                $.post("/cart/addCart",{weaponId:weaponId},function (data) {
                    console.log(data);
                    if (data == "ok"){
                        alert("加购成功!")
                    }else {
                        alert("当前加入购物车人数过多,请稍后再试!");
                    }
                })
            }
        },
        created() {
            this.weaponListInfo();
        }
    })
</script>

<div th:insert="publicFooter.html"></div>

</body>
</html>